<?php 
class boutique_gerer extends MX_Controller{
	public function modifier($id=0){
		
		$boutiqueInfo= $this->load->Module('boutique/show');		
		$boutique=$boutiqueInfo->get_info($id);
		
		if(!$boutique){
			$data=array(
					'title'=>"Cette boutique est désactivée",
					'description'=>"boutique non-trouvable",
					'keywords'=>''
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'boutique-desactive',
							'footer'),
									$data); // Display the page
			return false;
		}
		$title="Vous voulez modifier votre boutique ";
		$description="modifier la boutique sur Caverne";
		$keywords = "modifier une boutique, boutique gratuiment, modifier boutique Caverne";
		
			$category= $this->load->module('category/index');
			$annonce_category=$category->dropdown_option_annonce_cats($boutique->category_id);
		
			$localisation= $this->load->module('localisation/index');
			$location=$localisation->getlocation();
			$location->getlocationCodePostal($boutique->code_postal);
			$list_departement=$location->OptionDepartement();
			$regionList=$location->OptionRegion();
			
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'departementList'=>$list_departement,
				'regionList'=>$regionList,				
				'annonce_category'=>$annonce_category,
				'boutique'=>$boutique);
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$data['nav_class']['boutique']='active';
		
		$this->view(
				array('header',
					'boutique-modifier',
					'annonce-deposer-footer'),
									$data); // Display the page
	}
	public function modifier_boutique(){
		ini_set('display_errors', 1);
		$title="Vous modifiez votre boutique ";
		$description="modifier boutique sur Caverne";
		$keywords = "modifier une boutique, modifier boutique Caverne";
		
		$setting=$this->load->Module('setting/data');

		$shopInfo= $this->load->Module('boutique/show');
		
		$id=$this->input->post('id');
		$shop=$shopInfo->get_info($id);
		$message=array();
		if($shop->password==$this->input->post('password')){
			$this->load->helper('url');
			$this->load->helper('text');
			$message[]="Vos changements ont été enregistrés";
			$shop=new stdclass;
			$shop->id= $id;
			$shop->company_name= $this->input->post('nom_boutique');
			$shop->shop_link= url_title(convert_accented_characters($this->input->post('nom_boutique')));
			$shop->email= $this->input->post('email');//todo
			$shop->tilte= $this->input->post('tilte');
			$shop->subtilte= $this->input->post('subtilte');
			$shop->description= $this->input->post('description');
			$shop->shop_tag= $this->input->post('shop_tag');
			$shop->shop_keywords= $this->input->post('shop_keywords');
			//$shop->date_create= strtotime('now');
				
			$shop->siren= $this->input->post('siren');
			$shop->adresse= $this->input->post('adresse');
			$shop->region= $this->input->post('region');
			$shop->departement= $this->input->post('departement');
			$shop->code_postal= $this->input->post('code_postal');
			
			$shop->frist_name= ucwords(strtolower($this->input->post('nom')));
			$shop->last_name= strtoupper($this->input->post('prenom'));
			$shop->site= $this->input->post('site');
			$shop->open= $this->input->post('horaies');
			$shop->category_id= $this->input->post('category');
			$shop->telephone= $this->input->post('telephone');
			$shop->youtube= $this->input->post('youtube');
				
			$photos=$this->input->post('photo');
			$photo_current=$this->input->post('logo_current');
			if($photos)
				$shop->photo=self::replacer_Photo(FCPATH."images",'logos',$photos,$photo_current);
		
			$this->model('shop_models_admin')->edit($shop);
			$shop=$shopInfo->get_info($id);
		}else{
			$message[]="<h3 class='text-orange'>Votre mot de passe n'est pas validé</h3>";
			$message[]="<a  href='modifier/$id' class='btn btn-primary btn-small'>Retourner la page modifier</a>";
		}
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'message'=>$message,
				'boutique'=>$shop);
		$setting=$this->load->Module('setting/data');
		$data=$setting->data($data);
		$data['nav_class']['boutique']='active';
		$this->view(
				array('header',
						'boutique-modifier-confirme',
						'boutique-info',
						'footer'),
							$data); // Display the page
	}

	private function replacer_Photo($path,$cat,$photos=array(),$photo_current=''){
		
		if($photo_current!=''){
			if(file_exists("$path/$cat/$photo_current")) unlink("$path/$cat/$photo_current");
			if(file_exists("$path/$cat/thumbnails/$photo_current")) unlink("$path/$cat/thumbnails/$photo_current");
		}
		
		if(!empty($photos))
		foreach ($photos as $photo){
			$temp_photo="$path/$photo";
			$temp_thumb_photo="$path/thumbnails/".$photo;
				
			//$NameCatPhoto=$cat."/$photo";
			$NameCatPhoto=$photo;
			if(!is_dir("$path/$cat"))mkdir("$path/$cat", 0777);
			if(!is_dir($path."/thumbnails/".$cat))mkdir($path."/thumbnails/".$cat, 0777);
				
			if (@copy($temp_photo,"$path/$NameCatPhoto")) unlink($temp_photo);
			if (@copy($temp_thumb_photo,"$path/$cat/thumbnails/$photo")) unlink($temp_thumb_photo);
	
			$product_photo[]=$NameCatPhoto;
		}
		if(isset($product_photo)) return $product_photo;
	
		return false;
	}
	public function supprimer($id=0){
	
		$setting=$this->load->Module('setting/data');
		$boutiqueInfo= $this->load->Module('boutique/show');
		$boutique=$boutiqueInfo->get_info($id);
	
		if(!$boutique){
			
			$data=array(
					'title'=>"Cette boutique est désactivée",
					'description'=>"boutique non-trouvable",
					'keywords'=>'',
			);
		$data=$setting->data($data);
		
			$this->view(
					array('header',
							'boutique-desactive',
							'footer'),
							$data); // Display the page
					return false;
		}
	
		$data=array(
				'title'=>"Supprimer la boutique",
				'description'=>"Supprimer la boutique",
				'keywords'=>'',
				'boutique'=>$boutique
		);
		$data=$setting->data($data);
		$this->view(
				array('header',
						'boutique-supprimer',
						'boutique-info',
						'annonce-deposer-footer'),
					$data); // Display the page
	}

	public function supprimer_boutique(){

		
		$setting=$this->load->Module('setting/data');
		$shopInfo= $this->load->Module('boutique/show');
		$id=$this->input->post('id');
		$shop=$shopInfo->get_info($id);
		
		if(!$shop){
				
			$data=array(
					'title'=>"Cette boutique est désactivée",
					'description'=>"boutique non-trouvable",
					'keywords'=>'',
			);
			$data=$setting->data($data);
		
			$this->view(
					array('header',
							'boutique-desactive',
							'footer'),
									$data); // Display the page
			return false;
		}
		if($shop->password==$this->input->post('password')){
			$message[]="Votre boutique a été supprimé définitive";
			$this->load->Module('shop/admin');
			$this->shop_admin->delete($id);
		}else{
			$message[]="<h3 class='text-orange'>Votre mot de passe n'est pas validé</h3>";
			$message[]="<a href='supprimer/$id' class='btn btn-primary '>Retourner à la page supprimer</a>";
		}
		
		$data=array(
				'title'=>"Supprimer la boutique",
				'description'=>"Supprimer la boutique",
				'keywords'=>'',
				'boutique'=>$shop,
				'message'=>$message
		);
		$data=$setting->data($data);
		$this->view(
				array('header',
						'boutique-supprimer-confirme',
						'boutique-info',
						'footer'),
				$data); // Display the page
	}
	public function active($shop_link=''){

		$boutique=false;
		$title=$description=$keywords = '';
		$id=$this->model('boutique_models_show')->active_id_from_link($shop_link);
		$message="la boutique n'a pas posté ";
		$search='';
		$setting=$this->load->Module('setting/data');

		
		if($id){
			$shopInfo= $this->load->Module('boutique/show');
			$boutique=$shopInfo->get_info($id);
		}
		
		if($boutique){
			
			$boutique->site_link='';
			if($boutique->site){
				$boutique->site_link=$boutique->site;
				$site=parse_url($boutique->site);
				$scheme=isset($site['scheme'])?$site['scheme']:'http';
				$host=isset($site['host'])?$site['host']:'';
				$user=isset($site['user'])?$site['user']:'';
				$pass=isset($site['pass'])?':'.$site['pass'].'@':'';
				$path=isset($site['path'])? $site['path']:'';
				$query=isset($site['query'])?'?'.$site['query']:'';
				$fragment=isset($site['fragment'])?'#'.$site['fragment']:'';
				
				$boutique->site="<a href='$scheme://$user$pass$host$path$query$fragment'>{$host}</a>";
			}

			$title=$boutique->company_name.' : '.$boutique->tilte;
			$this->load->helper('text');
			$description=word_limiter(str_replace('"',' ',strip_tags($boutique->description)), 250);
			$this->load->helper('string');
			$keywords = preg_split("/[\s,]+/",$boutique->tilte);
			if(is_array($keywords))$keywords = reduce_multiples(implode(',', $keywords),",");
		}else{
			$data=array(
					'title'=>"Cette boutique est désactivée",
					'description'=>"boutique non-trouvable",
					'keywords'=>''
			);
			$data=$setting->data($data);
			$this->view(
					array('header',
							'boutique-desactive',
							'footer'),
							$data); // Display the page
			return false;
		}
		$data=array(
				'title'=>$title,
				'description'=>$description,
				'keywords'=>$keywords,
				'message'=>$message,
				'search_annonce'=>$search,
				'boutique'=>$boutique);
		$data=$setting->data($data);
		$data['nav_class']['boutique']='active';
		$this->view(
					array('boutique-header',
							'boutique-info',
							'footer'),
							$data); // Display the page
	}
	public function ajax_send_password(){
		$id=$this->input->post('id');
		$shopInfo= $this->load->Module('boutique/show');
		$shop=$shopInfo->get_info($id);
		if(isset($shop->password)){
			$email= $this->load->Module('send/email');
			$setting=$this->load->Module('setting/admin');
			$edition=$this->load->Module('setting/edition');
			$setting_admin=$setting->get_admin();
			$edition_send_password=$edition->get_Edition('send_password_boutique');
			$from_email=$setting_admin['admin_email'];
			$from_name='Caverne.fr';
			$to_email=$shop->email;
			$subject="Mot de passe de votre boutique: ".$shop->company_name;
			$message=$edition_send_password[0]->setting;
			$message_data=(array)$shop;
			foreach ($message_data as $key=>$data){
				if(is_string ($data)) $message=str_replace('{'.$key.'}',$data,$message);
			}
			$status='send';
			$action='send_password_boutique';
			$description='';
				
			$email->add($from_email,$from_name,$to_email,$subject,$message,$status,$action,$description);
			echo '<p class="text-orange">Votre mot de passe n\'est pas correct</p> <label for="email">Mot de passe oubli� ? </label> <input id="oublie_pw" type="button" value="Envoyer par email" />';
		}else{
			echo "votre référence n'est pas correct";
		}
	}
}